package com.yjt.admin.system.controller;


import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.yjt.admin.system.constant.SysCons;
import com.yjt.admin.system.entity.Emply;
import com.yjt.admin.system.form.EmplyForm;
import com.yjt.admin.system.param.EmplyParam;
import com.yjt.admin.system.service.EmplyService;
import com.yjt.admin.system.vo.EmplyVO;
import com.yjt.common.constant.ComConstant;
import com.yjt.common.enums.ModulEnum;
import com.yjt.common.enums.OperTypeEnum;
import com.yjt.common.log.annotation.OperLog;
import com.yjt.common.utils.SecurityUtils;
import com.yjt.common.vo.CommonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.security.crypto.bcrypt.BCrypt;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 员工表 前端控制器
 * </p>
 *
 * @author zzp
 * @since 2021-10-11
 */
@RefreshScope // 支持Nacos的动态刷新功能
@RestController
@RequestMapping("/admin/system/emply")
@Api(tags = "人员管理")
public class EmplyController {

    @Autowired
    private EmplyService emplyService;

    @PostMapping("/add")
    @ApiOperation(value = "增加人员信息", notes = "返回JSONObject")
    @OperLog(operModul = ModulEnum.EMPLY_MODUL, operType = OperTypeEnum.ADD, operDesc = "新增人员信息")
    public CommonResult addEmply(@RequestBody @Validated EmplyForm emplyForm) {
        emplyService.addEmply(emplyForm);
        return CommonResult.success("人员新增成功");
    }


    @DeleteMapping("/delete")
    @ApiOperation(value = "删除人员信息", notes = "返回JSONObject", hidden = true)
    @ApiImplicitParam(name = "emplyId", value = "人员id")
    @OperLog(operModul = ModulEnum.EMPLY_MODUL, operType = OperTypeEnum.DEL, operDesc = "删除人员信息")
    public CommonResult delEmply(String emplyId) {
        emplyService.removeById(emplyId);
        return CommonResult.success("人员删除成功");
    }

    @PutMapping("/update")
    @ApiOperation(value = "修改人员信息", notes = "返回JSONObject")
    @OperLog(operModul = ModulEnum.EMPLY_MODUL, operType = OperTypeEnum.MOD, operDesc = "修改人员信息")
    public CommonResult updateEmply(@RequestBody @Validated EmplyForm emplyForm) {
        emplyService.updateEmply(emplyForm);
        return CommonResult.success("人员修改成功");
    }


    @GetMapping("/{emplyId}")
    @ApiOperation(value = "查询人员信息", notes = "返回JSONObject")
    @ApiImplicitParam(name = "emplyId", value = "人员id")
    public CommonResult<EmplyVO> getOneEmply(@PathVariable String emplyId) {
        return CommonResult.success(emplyService.getOneEmply(emplyId), "人员信息查询成功");
    }


    @PostMapping("/list-page")
    @ApiOperation(value = "查询分页人员信息", notes = "返回JSONObject")
    public CommonResult<IPage<EmplyVO>> getPageEmply(@RequestBody EmplyParam emplyParam) {
        return CommonResult.success(emplyService.getPageEmply(emplyParam), "查询人员分页信息成功");
    }


    @PutMapping("/forbid")
    @ApiOperation(value = "禁用人员", notes = "返回JSONObject")
    @OperLog(operModul = ModulEnum.EMPLY_MODUL, operType = OperTypeEnum.MOD, operDesc = "禁用人员")
    @ApiImplicitParam(name = "emplyId", value = "人员id", dataType = "string")
    public CommonResult stopEmply(String emplyId) {
        //构造参数
        LambdaUpdateWrapper<Emply> lambdaUpdateWrapper = Wrappers.<Emply>lambdaUpdate()
                .eq(Emply::getEmplyId, emplyId)
                .set(Emply::getIsForbid, 0);
        emplyService.update(lambdaUpdateWrapper);
        return CommonResult.success("人员禁用成功");
    }

    @PutMapping("/reset/{emplyId}")
    @ApiOperation(value = "重置员工密码", notes = "返回JSONObject")
    @ApiImplicitParam(name = "emplyId", value = "人员id")
    public CommonResult<EmplyVO> resetEmplyPwd(@PathVariable String emplyId) {
        String bcyPassword = BCrypt.hashpw(SysCons.DEFAULT_PASSWORD, BCrypt.gensalt());
        //构造参数
        LambdaUpdateWrapper<Emply> lambdaUpdateWrapper = Wrappers.<Emply>lambdaUpdate()
                .eq(Emply::getEmplyId, emplyId)
                .set(Emply::getPwd, bcyPassword);
        emplyService.update(lambdaUpdateWrapper);
        return CommonResult.success("密码重置成功");
    }

    @GetMapping("isModPassword")
    @ApiOperation(value = "判断是否需要修改密码", notes = "返回JSONObject")
    public CommonResult<Boolean> selectUserPassword() {
        String emplyId = SecurityUtils.getCurrentUser().getEmplyId();
        Emply emply = this.emplyService.getById(emplyId);
        boolean checkpw = BCrypt.checkpw(SysCons.DEFAULT_PASSWORD, emply.getPwd());
        return CommonResult.success(checkpw);
    }

}
